<%@ page language="java" pageEncoding="UTF-8"%>
<%@page import="com.shsh.safemanage.model.Role"%>
<%@page import="com.shsh.safemanage.action.ActionCreator"%>
<%@page import="com.shsh.safemanage.action.UserAction"%>
<%@page import="com.shsh.safemanage.dao.EquipmentDao"%>
<%@page import="com.shsh.safemanage.model.Equipment"%>
<%@page import="com.shsh.safemanage.model.User"%>
<%@page import="com.shsh.safemanage.dao.UserDao"%>
<%@page import="com.shsh.safemanage.model.ProblemTransferFeedback"%>
<%@page import="com.shsh.safemanage.util.Constants"%>
<%@page import="java.util.HashSet"%>
<%@page import="java.util.Set"%>
<%@page import="com.shsh.safemanage.dao.DepartmentDao"%>
<%@page import="com.shsh.safemanage.model.Department1"%>
<%@page import="com.shsh.safemanage.model.ProblemTransfer"%>
<%@page import="java.util.List"%>
<%@page import="com.shsh.safemanage.util.Util"%>
<%@page import="com.shsh.safemanage.model.Problem"%>
<%@page import="com.shsh.safemanage.dao.DaoCreator"%>
<%@page import="com.shsh.safemanage.dao.ProblemDao"%>

<%
{
	String theProblemIDStr = request.getParameter(Constants.REQUEST_PARAMETER_ID);
	Long theProblemID = 0L;
	try {
		theProblemID = new Long(theProblemIDStr);
	} catch (Exception e) {
	}
	
	UserAction theUserAction = ActionCreator.createUserAction();

	DepartmentDao theDepartmentDao = DaoCreator.createDepartmentDao();
	ProblemDao theProblemDao = DaoCreator.createProblemDao();
	HSEProblemDao hseProblemDao = DaoCreator.createHSEProblemDao();
	
	UserDao theUserDao = DaoCreator.createUserDao();
	Problem theProblem = theProblemDao.getProblemByID(theProblemID);
	request.setAttribute(Constants.REQUEST_ATTRIBUTE_PROBLEM, theProblem);
	
	User theLoginUser = (User) session.getAttribute(Constants.SESSION_USER);
	
	List<ProblemTransfer> transferList = theProblemDao.getProblemTransferByProblemID(theProblemID);
	List<ProblemTransferFeedback> feedbackList = theProblemDao.getProblemTransferFeedbackByProblemID(theProblemID);
	
	if (theProblem.getProcessStatus() >= Constants.PROBLEM_STATUS_CLOSE) {
		request.setAttribute(Constants.REQUEST_ATTRIBUTE_IS_READONLY, Boolean.TRUE);
	} else {
		request.setAttribute(Constants.REQUEST_ATTRIBUTE_IS_READONLY, Boolean.FALSE);
	}
	List<ProblemHSECategory> problemHSECategoryList = hseProblemDao.getProblemHSECategoryList(theProblem.getId());
	if (problemHSECategoryList.size() > 0) {
		request.setAttribute(Constants.REQUEST_ATTRIBUTE_PROBLEM_HSE_CATEGORY_LIST, problemHSECategoryList);
	} else {
		request.setAttribute(Constants.REQUEST_ATTRIBUTE_PROBLEM_HSE_CATEGORY_LIST, null);
	}
	request.setAttribute(Constants.REQUEST_ATTRIBUTE_PROBLEM, theProblem);
%>
<table align="center" border="1" width="99%" cellpadding="0"
	cellspacing="0">
	<tr>
		<td class="td1">
			<span style="margin-left: 30px; font-weight: bold">流程跟踪：</span>
			<br />
			<span style="margin-left: 50px; color: red">
			<%=Util.formatDateTime(theProblem.getWriteTime())%> <%=theProblem.getWriteUserName()%> 填报问题
			</span>
			<%
			for (ProblemTransferFeedback feedback : feedbackList) {
			%>
			<br />
			<span style="margin-left: 50px; color: red">
			<%=Util.formatDateTime(feedback.getFeedbackTime())%>
			<%
			User theFeedbackUser = theUserDao.getUserByID(feedback.getUserID());
			Role theFeedbackUserRole = theUserAction.getUserRole(theFeedbackUser);
			%>
			<%=theFeedbackUser.getName()%>
			<%=((theFeedbackUserRole.getRoleType() == Constants.ROLE_TYPE_BUZHANG || theFeedbackUserRole.getRoleType() == Constants.ROLE_TYPE_CHUZHANG)
					&& (feedback.getProcessMethod() == Constants.PROCESS_METHOD_ESCALATE))? "<strong>同意</strong>":""%><%=feedback.getProcessMethodStr()%>
			</span>
			<%
			}
			%>
		</td>
	</tr>
	<tr>
		<td class="td1"><span
			style="margin-left: 30px; font-weight: bold">作业部（业务中心）：</span> <span><%=theProblem.getDepartment1Name()%></span>
			<span style="margin-left: 30px; font-weight: bold">装置：</span> <span><%=theProblem.getEquipmentName()%></span>
		</td>
	</tr>
	<tr>
		<td class="td1"><span
			style="margin-left: 30px; font-weight: bold">通报内容标题：</span> <%=theProblem.getProblemTitle()%>
		</td>
	</tr>
	<tr>
		<td class="td1"><span
			style="margin-left: 30px; font-weight: bold">通报内容：</span> <br /> <span
			style="margin-left: 30px">问题内容：</span> <%=theProblem.getProblemContent()%>
			<br /> <span style="margin-left: 30px">整改措施：</span> <%=theProblem.getProblemZhenggai()%>
		</td>
	</tr>
	<tr>
		<td class="td1">
			<%
			Department1 writeUserDepart1 = theDepartmentDao.getDepart1ByUserID(theProblem.getWriteUserID());
			%>
			<span style="margin-left: 30px; font-weight: bold">通报人及时间：</span>
			<span><%=(writeUserDepart1 == null)? "":writeUserDepart1.getDepartmentName()%></span>
			<span style="margin-left: 20px"><%=theProblem.getWriteUserName()%></span>
			<span style="margin-left: 20px"><%=Util.formatDateTime(theProblem.getWriteTime())%></span>
		</td>
	</tr>
	<%
	if (theProblem.getProblemType() == null || theProblem.getProblemType().equals(0)) {
	%>
	<tr id="hseCategoryTR">
		<td class="td1" height="35">
			<div id="hseCategoryDiv">
			<%@include file="problem_hse_category_section.jsp" %>
			</div>
		</td>
	</tr>
	<%
	}
	%>
	<%
		Set<String> nameSet = new HashSet<String>();
		StringBuilder sb = new StringBuilder();
		for (ProblemTransfer transfer : transferList) {
			if (transfer.isFirstSent() && !transfer.getTransferedUserID().equals(theProblem.getWriteUserID())) {
				nameSet.add(transfer.getTransferedUserName());
			}
		}
		for (String name : nameSet) {
			if (sb.length() == 0) {
				sb.append(name);
			} else {
				sb.append(" " + name);
			}
		}
	%>
	<tr>
		<td class="td1"><span
			style="margin-left: 30px; font-weight: bold">问题被通知人：</span> <span><%=sb.toString()%></span>
		</td>
	</tr>
	<%
			for (ProblemTransferFeedback feedback : feedbackList) {
				User feedbackUser = theUserDao.getUserByID(feedback.getUserID());
	%>
	<tr>
		<td class="td1"><span
			style="margin-left: 30px; font-weight: bold">【<%=feedbackUser.getDepartment1Name()%>】
			</span> 
			<br /> 
			<span style="margin-left: 30px;"><%=feedback.getFeedback()%></span>
			<%
			if (feedback.getProcessMethod() == Constants.PROCESS_METHOD_TRANSFER && feedback.getTransferedUserNames() != null
				&& feedback.getTransferedUserNames().length() > 0) {
			%>
			<br />
			<span style="margin-left: 30px;">通知人：<%=feedback.getTransferedUserNames()%></span>
			<%
			}
			%>
			<br /> 
			<div style="margin-left: 30px;" align="right">
				<%=feedbackUser.getName()%>
				<span><%=Util.formatDateTime(feedback.getFeedbackTime())%></span></div>
		</td>
	</tr>
	<%
			}
	%>
</table>
<%
}
%>

